﻿using System;
using System.Data;
using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;
using System.Web;
using System.Web.Caching;


using LMD.Constant;
using LMD.Lib.Oracle;

namespace Cached
{
    /// <summary>
    /// Summary description for FeedSource
    /// </summary>
    public class FeedSource : CacheAbstraction
    {
        public DataTable GetFeedSourceList()
        {
            return GetFeedSourceList(false);
        }
        public DataTable GetFeedSourceList(bool bForceRefresh)
        {
            String sxCacheKey = KCache.FEED_SOURCE;

            if(bForceRefresh)
            {
                HttpRuntime.Cache.Remove(sxCacheKey);
            }

            if (HttpRuntime.Cache[sxCacheKey] == null)
            {
                DBLib dbl = new DBLib();

                String sxQuery =
                    @"
                    select
                        FEED_SOURCE_ID ID,
                        NAME,
                        URL,
                        case when nvl(VALID_TO, SysDate+1) > SysDate
                            then 'Y'
                        else 'N'
                        end VALIDITY
                    from 
                        FEED_SOURCE                    
                    order by
                        FEED_SOURCE_ID ASC
                    ";

                DataTable dt = dbl.GetDataTable(sxQuery);

                AddToCache(sxCacheKey, dt, CacheType.Daily);
                
            }
            return (DataTable)HttpRuntime.Cache[sxCacheKey];
        }        

		public DataTable GetOtherApps()
        {
            return GetOtherApps(false);
        }
        public DataTable GetOtherApps(bool bForceRefresh)
        {
            String sxCacheKey = KCache.OTHER_APPS;

            if (bForceRefresh)
            {
                HttpRuntime.Cache.Remove(sxCacheKey);
            }

            if (HttpRuntime.Cache[sxCacheKey] == null)
            {
                DBLib dbl = new DBLib();

                String sxQuery =
                    @"
                    select
                        FEED_SOURCE_ID ID,
                        NAME,
                        URL,
                        case when nvl(VALID_TO, SysDate+1) > SysDate
                            then 'Y'
                        else 'N'
                        end VALIDITY
                    from 
                        FEED_SOURCE
                    where
                        URL not like '%portal/icp%'
                        and
                        NAME not like '%ICP%'                    
                    order by
                        FEED_SOURCE_ID ASC
                    ";

                DataTable dt = dbl.GetDataTable(sxQuery);

                AddToCache(sxCacheKey, dt, CacheType.Daily);

            }
            return (DataTable)HttpRuntime.Cache[sxCacheKey];
        }
		
        public override String GetCacheFilename()
        {
            return KCache.Filename.FEED_SOURCE;
        }
    }
}